<template>
  <div class="app-container">
    <div class="filter-more">
      <div class="from_header">
        <el-form ref="form" label-width="120px" inline>
          <el-form-item label="客户名称：">
            <el-input
              v-model.trim="query.name"
              clearable
              size="small"
              placeholder="请输入客户名称"
              @clear="selected"
            />
          </el-form-item>
          <el-form-item label="客户代码：">
            <el-input
              v-model.trim="query.code"
              clearable
              size="small"
              placeholder="请输入客户代码"
              @clear="selected"
            />
          </el-form-item>

          <el-form-item label="">
            <el-button
              type="primary"
              size="small"
              @click="selected"
            >查询</el-button
            >
            <el-button
              type="default"
              size="small"
              @click="restList"
            >重置</el-button
            >
          </el-form-item>
        </el-form>

        <div class="establish">
          <el-button type="primary" size="small" @click="add">新增</el-button>
        </div>
      </div>
    </div>

    <el-table
      :data="list"
      :header-row-style="{
        'background-color': '#E6E6E6',
        'font-weight': 'bold',
        color: '#333'
      }"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      align="center"
      height="650"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
    >
      <el-table-column
        show-overflow-tooltip
        align="center"
        prop="code"
        label="客户代码"
      />
      <el-table-column
        show-overflow-tooltip
        align="center"
        prop="name"
        label="客户名称"
      />
      <el-table-column
        show-overflow-tooltip
        align="center"
        prop="createDt"
        label="创建日期"
      />
      <el-table-column fixed="right" align="center" label="操作">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="mini"
            @click="() => deleteRow(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      :current-page="query.page"
      :total="total"
      :page-size="query.limit"
      style="padding: 30px 0; text-align: end"
      layout="total, prev, pager, next,jumper"
      @current-change="fetchData"
    />

    <el-dialog
      :visible.sync="dialogFormVisible"
      :title="dialogFormValue"
      @close="closeDialog"
    >
      <el-form
        ref="add"
        :model="addForm"
        :rules="validateAddForm"
        label-width="120px"
      >
        <el-form-item label="客户名称：" prop="name">
          <el-col :span="8">
            <el-input
              v-model="addForm.name"
              size="small"
              placeholder="请输入客户名称"
            />
          </el-col>
        </el-form-item>
        <el-form-item label="客户代码：" prop="code">
          <el-col :span="8">
            <el-input
              v-model="addForm.code"
              size="small"
              placeholder="请输入客户代码"
            />
          </el-col>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="restData()">取 消</el-button>
        <el-button type="primary" @click="confirmAdd">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// { listManufacturer, updateManufacturer }
import user from '@/api/acl/user.js'

export default {
  data() {
    return {
      query: {
        name: '',
        code: '',
        page: 1,
        limit: 10
      },
      total: 1,
      list: [],
      loading: false,
      addForm: {
        name: '',
        code: ''
      },
      validateAddForm: {
        name: [
          { required: true, trigger: 'change', message: '请输入客户名称' }
        ],
        code: [
          { required: true, trigger: 'change', message: '请输入客户代码' }
        ]
      },
      dialogFormVisible: false,
      dialogFormValue: '新增客户'
    }
  },

  created() {
    this.fetchData()
  },

  methods: {
    restList() {
      this.query.page = 1
      this.query.name = ''
      this.query.code = ''
      this.getList()
    },
    restData() {
      this.dialogFormVisible = false
      //   this.dialogFormValue = "";
    },
    selected() {
      this.query.page = 1
      this.getList()
    },
    fetchData(val = 1) {
      this.query.page = val
      this.getList()
    },
    getList() {
      console.log('this.query: ', this.query)
      user.listCustomer(this.query).then(response => {
        this.total = response.total
        this.list = response.list
      })
    },
    deleteRow(val) {
      if (!val.id) return false
      this.$confirm('是否删除该客户吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          user.updateCustomer({ id: val.id, deleteFlag: 1 }).then(res => {
            this.$message({
              message: '删除成功',
              duration: 1000
            })
            this.getList()
          })
        })
        .catch(() => {})
    },
    add() {
      this.dialogFormVisible = true
    },
    confirmAdd() {
      this.$refs.add.validate(valid => {
        if (valid) {
          user.insertCustomer(this.addForm).then(res => {
            this.getList()
            this.$message({
              message: '新增成功',
              duration: 1000
            })
            this.dialogFormVisible = false
          })
        }
      })
    },
    closeDialog() {
      this.$refs.add.clearValidate()
      this.$refs.add.resetFields()
    }
  }
}
</script>

<style lang="scss">
.establish {
  text-align: right;
}
</style>
